package com.miplus.ccrm.workflow.controller;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.workflow.model.common.DeleteRequest;
import com.miplus.ccrm.workflow.model.dto.knowledge.*;
import com.miplus.ccrm.workflow.model.vo.KnowledgeBaseVO;
import com.miplus.ccrm.workflow.model.vo.KnowledgeQuestionVO;
import com.miplus.ccrm.workflow.model.vo.MatchRuleVO;
import com.miplus.ccrm.workflow.model.vo.MatchVO;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeBaseService;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeQuestionRuleService;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 知识库配置Controller
 */
@RestController
@RequestMapping("/knowledge")
@Api(tags = "知识库配置管理")
public class KnowledgeBaseController {

    @Resource
    private WorkflowKnowledgeBaseService workflowKnowledgeBaseService;
    @Resource
    private WorkflowKnowledgeQuestionService workflowKnowledgeQuestionService;
    @Resource
    private WorkflowKnowledgeQuestionRuleService workflowKnowledgeQuestionRuleService;

    /**
     * 创建知识库配置
     */
    @PostMapping("/base/create")
    @ApiOperation("创建知识库配置")
    public R<CmdResVo> createKnowledgeBase(@RequestBody KnowledgeBaseCreateDTO knowledgeBaseCreateDTO) {
        return R.Success(workflowKnowledgeBaseService.cereateKnowledgeBase(knowledgeBaseCreateDTO));
    }


    /**
     * 更新知识库配置
     */
    @PostMapping("/base/update")
    @ApiOperation("更新知识库配置")
    public R<CmdResVo> updateKnowledgeBase(@RequestBody KnowledgeBaseUpdateDTO knowledgeBaseUpdateDTO) {
        return R.Success(workflowKnowledgeBaseService.updateKnowledgeBase(knowledgeBaseUpdateDTO));
    }

    /**
     * 删除知识库配置
     */
    @PostMapping("/base/delete")
    @ApiOperation("删除知识库配置")
    public R<CmdResVo> deleteKnowledgeBase(@RequestBody DeleteRequest deleteRequest) {
        return R.Success(workflowKnowledgeBaseService.deleteKnowledgeBase(deleteRequest));
    }

    /**
     * 分页查询知识库配置列表
     */
    @PostMapping("/base/list")
    @ApiOperation("分页查询知识库配置列表")
    public R<PageData<KnowledgeBaseVO>> getKnowledgeBaseList(@RequestBody KnowledgeBaseQueryDTO query) {
        return R.SuccessPage(workflowKnowledgeBaseService.getKnowledgeBaseList(query));
    }

    /**
     * 查询可用知识库列表（状态为启用的知识库）
     */
    @GetMapping("/base/list/enable")
    @ApiOperation("查询可用知识库列表（状态为启用的知识库）")
    public R<List<KnowledgeBaseVO>> getEnableKnowledgeBaseList() {
        return R.Success(workflowKnowledgeBaseService.getEnableKnowledgeBaseList());
    }

    /**
     * 创建知识库的问题和匹配规则
     */
    @PostMapping("/question/create")
    @ApiOperation("创建知识库问题")
    public R<CmdResVo> createKnowledgeQuestion(@RequestBody KnowledgeQuestionCreateDTO questionDTO) {
        return R.Success(workflowKnowledgeQuestionService.cereateKnowledgeQuestion(questionDTO));
    }

    /**
     * 更新知识库问题
     */
    @PostMapping("/question/update")
    @ApiOperation("更新知识库问题")
    public R<CmdResVo> updateKnowledgeQuestion(@RequestBody KnowledgeQuestionUpdateDTO questionDTO) {
        return R.Success(workflowKnowledgeQuestionService.updateKnowledgeQuestion(questionDTO));
    }

    /**
     * 删除知识库问题
     */
    @PostMapping("/question/delete")
    @ApiOperation("删除知识库问题")
    public R<CmdResVo> deleteKnowledgeQuestion(@RequestBody DeleteRequest deleteRequest) {
        return R.Success(workflowKnowledgeQuestionService.deleteKnowledgeQuestion(deleteRequest));
    }

    /**
     * 分页查询某知识库问题列表
     */
    @PostMapping("/question/list")
    @ApiOperation("分页查询知识库问题列表")
    public R<PageData<KnowledgeQuestionVO>> getKnowledgeQuestionList(@RequestBody KnowledgeQuestionQueryDTO query) {
        return R.SuccessPage(workflowKnowledgeQuestionService.getKnowledgeQuestionList(query));
    }

    /**
     * 创建知识库问题的匹配规则
     */
    @PostMapping("/rule/create")
    @ApiOperation("创建匹配规则")
    public R<CmdResVo> createMatchRule(@RequestBody MatchRuleCreateDTO ruleDTO) {
        return R.Success(workflowKnowledgeQuestionRuleService.cereateMatchRule(ruleDTO));
    }

    /**
     * 更新匹配规则
     */
    @PostMapping("/rule/update")
    @ApiOperation("更新匹配规则")
    public R<CmdResVo> updateMatchRule(@RequestBody MatchRuleUpdateDTO ruleDTO) {
        return R.Success(workflowKnowledgeQuestionRuleService.updateMatchRule(ruleDTO));
    }

    /**
     * 删除匹配规则
     */
    @PostMapping("/rule/delete")
    @ApiOperation("删除匹配规则")
    public R<CmdResVo> deleteMatchRule(@RequestBody DeleteRequest deleteRequest) {
        return R.Success(workflowKnowledgeQuestionRuleService.deleteMatchRule(deleteRequest));
    }

    /**
     * 根据问题ID查询匹配规则列表
     */
    @GetMapping("/rule/list")
    @ApiOperation("根据问题ID查询匹配规则列表")
    public R<List<MatchRuleVO>> getMatchRuleList(@RequestParam Long questionId) {
        return R.Success(workflowKnowledgeQuestionRuleService.getMatchRuleList(questionId));
    }

    /**
     * 根据输入内容匹配知识库问题
     */
    @PostMapping("/match")
    @ApiOperation("根据输入内容匹配知识库问题")
    public R<MatchVO> matchKnowledgeQuestion(@RequestBody MatchDTO matchDTO) {
        // TODO: 实现根据输入内容匹配知识库问题逻辑
        // 优先使用正则匹配，再使用关键词匹配
        // 返回匹配到的第一个问题的内容，如果没有匹配到则返回默认提示信息
        return R.Success(workflowKnowledgeQuestionRuleService.matchKnowledgeQuestion(matchDTO));
    }
}